package cumt.kydd.authoringplatform.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cumt.kydd.authoringplatform.bean.User;
import cumt.kydd.authoringplatform.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static cumt.kydd.authoringplatform.controller.UserInfoUpdateController.*;


@Service
public class UserService
{
    public static User LoginInUser = new User();
    @Resource
    private UserMapper userMapper;
    public Map<String,Object> createAccount(User user)
    {
        Map<String,Object> resultMap=new HashMap<>();
        //检查账号是否存在
        User u;
        u=userMapper.selectUserByName(user.getName());
        if(u!=null)
        {
            resultMap.put("code",400);
            resultMap.put("message","账号已存在,注册失败");
        }
        else
        {
            String regex = "^.{6,20}$";
            String regex2 = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$";
            Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(user.getName());
            Pattern p2 = Pattern.compile(regex2, Pattern.CASE_INSENSITIVE);
            Matcher m2 = p2.matcher(user.getPassword());
            if (m.matches()){
                if (m2.matches()) {

                    //盐
                    String salt= RandomUtil.randomString(2);
                    //加密密码：原始密码+盐
                    String md5Pwd= SecureUtil.md5(user.getPassword()+salt);
                    //初始化账号信息
                    user.setSalt(salt);
                    user.setPassword(md5Pwd);
                    int result=userMapper.insertUser(user);
                    if(result>0)
                    {
                        resultMap.put("code",200);
                        resultMap.put("message","注册成功，将跳转至登录页！");
                    }
                    else
                    {
                        resultMap.put("code",400);
                        resultMap.put("message","注册失败，用户名已存在！");
                    }

                } else {
                    resultMap.put("code",400);
                    resultMap.put("message","注册失败，密码不符合规范:需要有六位以上数字和字母组成");
                }
            } else {
                resultMap.put("code",400);
                resultMap.put("message","注册失败，用户名不符合规范:需要有六位以上任意字符组成");
            }
        }
        return resultMap;
    }
    public  Map<String,Object> loginAccount(User user, HttpServletRequest request)
    {
        Map<String,Object> resultMap=new HashMap<>();


        User u;
        u=userMapper.selectUserByName(user.getName());
        if(u==null)
        {
            resultMap.put("code",400);
            resultMap.put("message","用户名不存在，请检查用户名");
            return  resultMap;
        }
        String md5Pwd=SecureUtil.md5(user.getPassword()+u.getSalt());
        if(!u.getPassword().equals(md5Pwd))
        {
            resultMap.put("code",400);
            resultMap.put("message","密码错误，请检查密码");
            return  resultMap;
        }
        resultMap.put("code",200);
        resultMap.put("message","登录成功，欢迎您");

        request.getSession().setAttribute("user",user);
//        System.out.println(request.getSession().getAttribute("user"));
        LoginInUser = (User) request.getSession().getAttribute("user");
        System.out.println("已登录账号:"+LoginInUser.getName());
        System.out.println("已登录账号的密码:"+LoginInUser.getPassword());
        return  resultMap;
    }

    public  Map<String,Object> updatePortrait() {
        Map<String,Object> resultMap=new HashMap<>();
//        user.setPortrait(UserPortrait);
        int result = userMapper.updatePortrait(UserPortrait,LoginInUser.getName());
        if(result>0)
        {
            resultMap.put("code",200);
            resultMap.put("message","上传头像成功！");
        }
        else
        {
            resultMap.put("code",400);
            resultMap.put("message","图片过大，上传失败！");
        }
        return resultMap;
    }

    public String sendPortrait(@RequestParam(required = false)String portrait) {
        portrait=userMapper.selectPortraitByName(LoginInUser.getName());
        return portrait;
    }

    public  Map<String,Object> updateInfo() {
        Map<String,Object> resultMap=new HashMap<>();
        int result = userMapper.updateInfo(User_info_email,User_info_org,User_info_profile,LoginInUser.getName());
        if(result>0)
        {
            resultMap.put("code",200);
            resultMap.put("message","更新信息成功！");
        }
        else
        {
            resultMap.put("code",400);
            resultMap.put("message","未知错误，更新失败！");
        }
        return resultMap;
    }

    public String sendInfoEmail(@RequestParam(required = false) String info_email) {
        info_email = userMapper.selectInfoEmailByName(LoginInUser.getName());
        return info_email;
    }

    public String sendInfoOrg(@RequestParam(required = false) String info_org) {
        info_org = userMapper.selectInfoOrgByName(LoginInUser.getName());
        return info_org;
    }

    public String sendInfoProfile(@RequestParam(required = false) String info_profile) {
        info_profile = userMapper.selectInfoProfileByName(LoginInUser.getName());
        return info_profile;
    }

    public  Map<String,Object> updateUserTel() {
        Map<String,Object> resultMap=new HashMap<>();
        int result = userMapper.updateInfoTel(User_info_tel,LoginInUser.getName());
        if(result>0)
        {
            String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
            Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(User_info_tel);
            if (m.matches()) {
                resultMap.put("code",200);
                resultMap.put("message","更新电话号码成功！");
            } else {
                resultMap.put("code",400);
                resultMap.put("message","请输入正确格式的电话号码！");
            }

        }
        else
        {
            resultMap.put("code",400);
            resultMap.put("message","未知错误，更新失败！");
        }
        return resultMap;
    }

    public String sendInfoTel(@RequestParam(required = false) String info_tel) {
        info_tel = userMapper.selectInfoTelByName(LoginInUser.getName());
        return info_tel;
    }

    public  Map<String,Object> updateUserPassword() {
        Map<String,Object> resultMap=new HashMap<>();
        User u;
        u=userMapper.selectUserByName(LoginInUser.getName());
        if (Objects.equals(LoginInUser.getPassword(),User_old_password)) {
//            String securityPassword = SecureUtil.md5(User_new_password+u.getSalt());
            if (Objects.equals(User_new_password, User_check_password)) {
                String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$";
                Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
                Matcher m = p.matcher(User_new_password);
                if (m.matches()) {
                    String securityPassword = SecureUtil.md5(User_new_password+u.getSalt());
                    int result = userMapper.updatePassword(securityPassword,LoginInUser.getName());
                    if(result>0) {
                        resultMap.put("code",200);
                        resultMap.put("message","更新密码成功！");
                    } else {
                        resultMap.put("code",400);
                        resultMap.put("message","未知错误，更新失败！");
                    }
                } else {
                    resultMap.put("code",400);
                    resultMap.put("message","新的密码需要长度大于6位，并由数字和字母组成！");
                }
            } else {
                resultMap.put("code",400);
                resultMap.put("message","新密码和确认密码不同，请确认！");
            }
        } else {
            resultMap.put("code",400);
            resultMap.put("message","原密码输入有误，请确认！");
        }
        return resultMap;
    }
}
